package com.dt.record

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import java.io.File

class MyAdapter(context: Context, it: List<String>, pa: List<String>) :
    BaseAdapter() {
    private val mInflater: LayoutInflater
    private val mIcon1: Bitmap
    private val mIcon2: Bitmap
    private val mIcon3: Bitmap
    private val mIcon4: Bitmap
    private val items: List<String>
    private val paths: List<String>
    private var mcheckable=false
    private var allCheck=0  //标记全选状态,1为全选,2为全不选,0为清除状态
    private var checkboxCount=0

    override fun getCount(): Int {
        return items.size
    }
    fun getCheckboxCount(): Int {
        return checkboxCount
    }


    override fun getItem(position: Int): Any {
        return items[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    fun showCheck(checkable: Boolean){
        mcheckable=checkable
        checkboxCount=0
    }
    fun setAllCheck(flag:Int){
        allCheck=flag
        if(flag == 2)
            checkboxCount=0

    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val holder: ViewHolder
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.file_item, null)
            holder = ViewHolder()
            holder.text = convertView.findViewById<View>(R.id.text) as TextView
            holder.icon = convertView.findViewById<View>(R.id.icon) as ImageView
            holder.check = convertView.findViewById<View>(R.id.check) as CheckBox
            convertView.tag = holder
        } else {
            holder = convertView.tag as ViewHolder
        }
        val f = File(paths[position])
        if (items[position] == "b1") {
            holder.text!!.text = "/"
            holder.icon!!.setImageBitmap(mIcon1)
            holder.check?.visibility=View.GONE
        } else if (items[position] == "b2") {
            holder.text!!.text = ".."
            holder.icon!!.setImageBitmap(mIcon2)
            holder.check?.visibility=View.GONE
        } else {
            holder.text!!.text = f.name
            if (f.isDirectory) {
                holder.icon!!.setImageBitmap(mIcon3)
            } else {
                holder.icon!!.setImageBitmap(mIcon4)
            }
            if(mcheckable) {
                holder.check?.visibility = View.VISIBLE

            }else {
                holder.check?.setChecked(false)
                allCheck = 0
                holder.check?.visibility = View.GONE
            }

            if (allCheck == 1){//全选
                holder.check?.setChecked(true)
            }
            else if(allCheck == 2) {//全不选
                holder.check?.setChecked(false)
            }
            checkboxCount++

        }
        return convertView!!
    }

    inner class ViewHolder {
        var text: TextView? = null
        var icon: ImageView? = null
        var check: CheckBox? =null
    }

    init {
        mInflater = LayoutInflater.from(context)
        items = it
        paths = pa
        mIcon1 = BitmapFactory.decodeResource(context.resources, R.drawable.icon_back)
        mIcon2 = BitmapFactory.decodeResource(context.resources, R.drawable.icon_back02)
        mIcon3 = BitmapFactory.decodeResource(context.resources, R.drawable.icon_fodler)
        mIcon4 = BitmapFactory.decodeResource(context.resources, R.drawable.icon_file)
    }
}